﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using LMU.FinalProject.ACL.P2P;
using System.Configuration;

namespace Agent2.LMU.FinalProject.ACL.P2P.Interface
{
    class UI
    {
        string agentName = "";

        string downloadOrUpload;
        string fileToDownload;
        string scenario;
        AgentClient ac;
        List<ACLP2PAgent.P2P.DownloadRequest> files;


        public void Init()
        {


            Start();

          


            loadUI();
         



        }

        private void loadUI()
        {
            Scenario();

            FileIO();

            Console.Read();

        }


        public void Start()
        {
            Console.WriteLine("Welcome to the ACLP2P!\n");
            Thread.Sleep(1000);

            Console.WriteLine("Please give me a name:");
            Thread.Sleep(1000);
            
            ac = new AgentClient(Console.ReadLine());
            

            Console.WriteLine("Thank you");
            Thread.Sleep(1000);

        }

        public void FileIO()
        {
            Console.WriteLine("Now how can I assist you?");

            Console.WriteLine("1) Download File");
            Console.WriteLine("2) Upload File");

            downloadOrUpload = Console.ReadLine();

            if (downloadOrUpload == "1")
            {
                Console.WriteLine("Okay I'll download a file for you.");

                Console.WriteLine("Please select a file.");

                ShowServerFiles();
            
           
               Console.WriteLine("Input the number of the file you woule like:");

               fileToDownload = Console.ReadLine();



               string[] fileName = files[int.Parse(fileToDownload)].FileName.Split('\\');
               
                string dir = ConfigurationSettings.AppSettings["folder"];

               ac.DownloadFile(fileName[2], dir, dir + "\\" + fileName[2], 0);
                

               Console.WriteLine("The file has been saved to {0}",dir);
            }
            else
            {
                Console.WriteLine("Okay I'll upload a file for you.");
                
                Console.WriteLine("What is the file name and path?");

                   ac.UploadFile(Console.ReadLine());

                   Console.WriteLine("Your file has been uploaded");
            }

         

           

        }

        public void Scenario()
        {
            Console.WriteLine("Please choose a scenario to simulate ");

            Console.WriteLine("1) Normal");

            Console.WriteLine("2) Heavy Network Traffic");

            Console.WriteLine("3) Large File");
            scenario = Console.ReadLine();

            if (scenario != "1")
                ac.helpRequired = true;

            Thread.Sleep(1000);
            

        }


        public void ShowServerFiles()
        {
        files =  ac.ServerFiles();
            for (int i =0; i< files.Count; i++)
            {
                Console.WriteLine("{0}){1}", i, files[i].FileName);

            }

        }
        public void SlowDownLoad()
        {
            //this will work well when the file is split don't implement this yet


        }


    }
}
